package com.sky.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;

import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class SetMealServiceImpl implements SetmealService {
	@Autowired
	private SetmealMapper setmealmapper;
	@Autowired
	private SetmealDishMapper setmealdishmapper;
	@Autowired
	private DishMapper dishmapper;
	 @Transactional
	public void InsertSetMeal(SetmealDTO setmealdto) {
		// TODO 自动生成的方法存根
		Long id = setmealdto.getId();
		//首先我们要先setmealdto的一些属性封装到setmeal上
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealdto, setmeal);
		//调用setmeal的Mapper进行数据交互
		setmealmapper.InsertSetMeal(setmeal);
		//接下来是setmeal_dish的添加
		//获取到SetMealDish的参数
		List<SetmealDish> setmealDishes = setmealdto.getSetmealDishes();
		//利用for循环进行dishId的添加
		for(SetmealDish s:setmealDishes)
		{
			s.setSetmealId(id);
		}
		//修改完后将其封装为SetmealDish
		//不用封装
		setmealdishmapper.insertSetmeal(setmealDishes);
	}
	@Override
	public PageResult page(SetmealPageQueryDTO setpagequerydto) {
		// TODO 自动生成的方法存根
		//首先进行分页
		PageHelper.startPage(setpagequerydto.getPage(), setpagequerydto.getPageSize());
		//调用mapper获取数据库内容，同时将其封装为total
		Page<SetmealVO> page = setmealmapper.pageQuery(setpagequerydto);
		Long total = page.getTotal();
		List<SetmealVO> records = page.getResult();
		return new PageResult(total,records);
	}
	@Override
	public SetmealVO GetById(Long id) {
		// TODO 自动生成的方法存根
		//利用mapper获取数据
		Setmeal setmeal = setmealmapper.GetById(id);
	    List<SetmealDish> setmealDishes = setmealdishmapper.getBySetmealId(id);

	    SetmealVO setmealVO = new SetmealVO();
	    BeanUtils.copyProperties(setmeal, setmealVO);
	    setmealVO.setSetmealDishes(setmealDishes);
	    System.out.println(setmeal.toString());
	    System.out.println(setmealVO.toString());
	    return setmealVO;
	}
	@Override
	@Transactional
	public void update(SetmealDTO setmealdto) {
		// TODO 自动生成的方法存根
		//首先修改setmeal
		Setmeal setmeal = new Setmeal();
		//将属性赋值给setmeal
		BeanUtils.copyProperties(setmealdto, setmeal);
		//调用mapper进行数据库交互
		setmealmapper.update(setmeal);
		//获取套餐id
		Long setmealId = setmeal.getId();
		//将之前的dishs删除
		setmealdishmapper.deletedishs(setmealId);
		//获取dishs的数组
		List<SetmealDish> setmealDishes = setmealdto.getSetmealDishes();
		//添加到setmealdish，并且添加修改setmeal_id
		for(SetmealDish s:setmealDishes)
		{
			s.setSetmealId(setmealId);
			System.out.println(s.toString());
		}
		setmealdishmapper.insertSetmeal(setmealDishes);
	}
	@Override
	@Transactional
	public void deletedishs(List<Long> ids) {
		// TODO 自动生成的方法存根
		//调用mapper进行数据库交互操作
		 ids.forEach(id -> {
		        Setmeal setmeal = setmealmapper.GetById(id);
		        log.info("{}",setmeal.getStatus());
		        if(StatusConstant.ENABLE == setmeal.getStatus()){
		            //起售中的套餐不能删除
		            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
		        }
		    });
		 //删除套餐表中的数据
	      setmealmapper.deleteddishs(ids);
		//删除setmeal_dish
		 ids.forEach(setmealId -> {
		        //删除套餐菜品关系表中的数据
		        setmealdishmapper.deletedishs(setmealId);
		    });
	}
	@Override
	public void StartOrStop(Integer status, Long id) {
		// TODO 自动生成的方法存根
		//首先判断是否满足该状态的条件1、如果dishs中没有，那么改套餐就无法起售
		//获取id的setmeal
		Setmeal setmeal = setmealmapper.GetById(id);
		//获取setmealId
		Long setmealId = id;
		//获取setmealId的所有对象
		List<SetmealDish> setmealdish = setmealdishmapper.getBySetmealId(setmealId);
		//利用dishId去查询是否还要这种菜品，若没有就不能起售
		if(StatusConstant.ENABLE == status)
		{
			for(SetmealDish s:setmealdish)
			{
				if(dishmapper.getById(s.getDishId())==null)
				{
					throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
				}
			}
		}
		setmeal.setStatus(status);
		//如果可以就改变
		setmealmapper.update(setmeal);
	}

}
